﻿using System;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using Microsoft.Win32;

using Elegant.Ui;

namespace iPdf
{
    partial class MainForm : Form
    {
        #region Fields
        private static bool cancelAction = false;
        //we assume that in the begining, the current tab changes from empty tab to the first tab
        private bool tabPageChanging = true;
        private bool firstMinimize = true;
        private bool loaded = false;
        
        private TataRibbonPage currentPage;
        private TataRibbonPage previousPage;

        private WorkStatus status = WorkStatus.Idle;

        private delegate string FileDialogCaller(FileFormat[] formats, string defaultFileName);
        private delegate string ChooseDirectoryDialogCaller();

        //the timer for idle state
        private Timer idleTimer;

        private FormWindowState oldWindowState = FormWindowState.Maximized;

        private Collection<RibbonGroup> commonGroups = new Collection<RibbonGroup>();

        private Previewer previewer;

        //some ribbon page will take sometime to load up its controls, 
        //so if put it into the constructor will significantly slow the startup 
        //speed. so we install a run once timer to load the controls of the 
        //ribbon pages
        private System.Timers.Timer loadTimer;

        //if the user is confused, we need to show some help info
        private Timer helpInfoTimer;

        #endregion

        #region Properties
        public WorkStatus Status
        {
            get
            {
                return status;
            }
            set
            {
                if (status == WorkStatus.TaskImported && value == WorkStatus.Idle)
                {
                    //The imported task has been cancelled, show a balloon tip to tell the user
                    ShowNotification(global::iPdf.Properties.Resources.ImportedTaskCancelled);
                }
                status = value;
            }
        }

        public bool Busy
        {
            get
            {
                return status == WorkStatus.Busy;
            }
        }

        public TataGrid DataGrid
        {
            get
            {
                return Grid;
            }
        }

        public RecentFiles RecentFiles
        {
            get
            {
                return RecentItems;
            }
        }


        private ItemRetrievor.TataSearchOption SearchOption
        {
            get
            {
                ItemRetrievor.TataSearchOption searchOption = ItemRetrievor.TataSearchOption.None;
                if (SearchSubFolder.Checked)
                {
                    searchOption |= ItemRetrievor.TataSearchOption.SearchSubFolders;
                }
                if (SearchHiddenFiles.Checked)
                {
                    searchOption |= ItemRetrievor.TataSearchOption.SearchHiddenFiles;
                }

                return searchOption;
            }
        }

        public FileFormat Format
        {
            get
            {
                TataRibbonPage curPage = (TataRibbonPage)Ribbon.CurrentTabPage;
                return curPage.SourceFormat;
            }
        }

        public TataRibbonPage CurrentPage
        {
            get
            {
                return currentPage;
            }
        }

        public TataRibbonPage PreviousPage
        {
            get
            {
                return previousPage;
            }
        }

        public RibbonTabPageCollection TabPages
        {
            get
            {
                return Ribbon.TabPages;
            }
        }

        public bool TabPageChanging
        {
            get
            {
                return tabPageChanging;
            }
        }

        public static bool ActionCancelled
        {
            get
            {
                return cancelAction;
            }
            set
            {
                cancelAction = value;
            }
        }

        public int ItemsCount
        {
            get
            {
                return Grid.RowsCount - 1;
            }
        }

        public bool IsForegroundWindow
        {
            get
            {
                return WinApi.GetForegroundWindow() == this.Handle;
            }
        }

        public ApplicationMenu RibbonMenu
        {
            get
            {
                return OrbMenu;
            }
        }

        public string DefaultOutputFolder
        {
            get
            {
                return this.SetOutputFolder.DefaultOutputFolder;
            }
        }


        public bool IsRunningInBackground
        {
            get
            {
                return !this.ShowInTaskbar || !this.IsForegroundWindow;
            }
        }

        public bool IsPdfCreatorBusy
        {
            get
            {
                return PdfCreator.InUse || PdfCreator.Running;
            }
        }

        public Collection<RibbonGroup> CommonGroups
        {
            get
            {
                return this.commonGroups;
            }
        }

        public Previewer Previewer
        {
            get
            {
                if (this.previewer == null)
                {
                    InitializePreviewer();
                    FitClientControl(this.previewer);
                }
                return this.previewer;
            }
        }

        public SessionsManager Sessions
        {
            get
            {
                return this.SessionsManager;
            }
        }

        public PdfLetOption PdfLets
        {
            get
            {
                PdfLetOption pdflet = new PdfLetOption();
                pdflet.Bookmark = Bookmark.Checked;
                pdflet.Hyperlink = Hyperlink.Checked;
                pdflet.Navigator = Navigator.Checked;
                return pdflet;
            }
            set
            {
                Bookmark.Enabled = value.Bookmark;
                Hyperlink.Enabled = value.Hyperlink;
                Navigator.Enabled = value.Navigator;
            }
        }

        #endregion

        #region Ctor
        private void InstallHandlers()
        {
            #region Add application commands' handlers
            ApplicationCommands.AddFiles.Executed += new CommandExecutedEventHandler(AddItems_Executed);
            ApplicationCommands.AddFolder.Executed += new CommandExecutedEventHandler(AddItems_Executed);
            ApplicationCommands.Start.Executed += new CommandExecutedEventHandler(Start_Executed);
            ApplicationCommands.Stop.Executed += new CommandExecutedEventHandler(Stop_Executed);
            ApplicationCommands.RemoveSelection.Executed += new CommandExecutedEventHandler(RemoveSelection_Executed);
            ApplicationCommands.RemoveAll.Executed += new CommandExecutedEventHandler(RemoveAll_Executed);
            ApplicationCommands.MoveUp.Executed += new CommandExecutedEventHandler(MoveUp_Executed);
            ApplicationCommands.MoveDown.Executed += new CommandExecutedEventHandler(MoveDown_Executed);
            ApplicationCommands.Exit.Executed += new CommandExecutedEventHandler(Exit_Executed);
            ApplicationCommands.HowToReportBugs.Executed += new CommandExecutedEventHandler(HowToReportBugs_Executed);
            #endregion
        }

        private void InstallIdleTimer()
        {
            const int IdleTimerInterval = 300; //0.3s
            idleTimer = new Timer();
            idleTimer.Interval = IdleTimerInterval;
            idleTimer.Tick += new EventHandler(IdleTimer_Tick);
            idleTimer.Enabled = true;
        }

        private void InstallHelpInfoTimer()
        {
            const int HelpInfoTimerInterval = 25000; //25s
            helpInfoTimer = new Timer();
            helpInfoTimer.Interval = HelpInfoTimerInterval;
            helpInfoTimer.Tick += new EventHandler(HelpInfoTimer_Tick);
            helpInfoTimer.Enabled = true;
            helpInfoTimer.Start();
        }

        private void InstallLoadTimer()
        {
            const double LoadTimerInterval = 300;//0.3s 
            loadTimer = new System.Timers.Timer(LoadTimerInterval);
            loadTimer.Enabled = true;
            loadTimer.AutoReset = false;
            loadTimer.Elapsed += new System.Timers.ElapsedEventHandler(LoadTimer_Elapsed);
        }

        /// <summary>
        /// The constructor
        /// </summary>
        public MainForm()
        {
            Global.MainWindow = this;

            //Use auto mode to load the state of ribbon controls
            PersistentStateManager.LoadStateAutomaticallyFromIsolatedStorageForDomain = true ;

            //Initialize common controls
            InitializeCommonGroups();

            this.RecentItems = new RecentFiles();
            //Initialize component
            InitializeComponent();
            InstallHandlers();

            //Set current page and previous page
#if !LITE
            if (PdfCreator.Exists)
            {
                if (OfficeChecker.Exists)
                {
                    currentPage = this.OfficePage;
                }
                else
                {
                    currentPage = this.PdfToXPage;
                }
            }
            else
            {
                currentPage = (TataRibbonPage)this.ImageToPdfPage;
            }
#else
            currentPage = this.ImageToPdfPage;
#endif

            previousPage = null;

            //Load common controls
            LoadCommonControls();
            //Load output folder history
            SetOutputFolder.Load();

            this.OrbMenu.Showing += new EventHandler(
                delegate(object sender, EventArgs e)
                {
                    UpdateOrbMenu();
                    RecentItems.Format = Format;
                    RecentItems.Update();
                });

            this.KeyPreview = true;
            this.OrbMenu.ProcessDialogKey += new DialogKeyEventHandler(
                delegate(object sender, DialogKeyEventArgs e)
                {
                    RecentItems.ProcessKey(e.KeyData);
                });

            MaximizeWindow();

            UpdateSessionsPanel();
            UpdatePageIntroInfo();
        }

        /// <summary>
        /// Maximize the window
        /// </summary>
        private void MaximizeWindow()
        {
            this.Size = SystemInformation.PrimaryMonitorSize;
        }


        /// <summary>
        /// Initialize the common ribbon controls include Add group, 
        /// Move group, Option group, Action group
        /// </summary>
        private void InitializeCommonGroups()
        {
            #region Create controls 
            this.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));

            this.AddGroup = new Elegant.Ui.RibbonGroup();
            this.AddFiles = new Elegant.Ui.Button();
            this.AddFolder = new Elegant.Ui.Button();
            this.SearchSubFolder = new Elegant.Ui.CheckBox();
            this.SearchHiddenFiles = new Elegant.Ui.CheckBox();

            this.MoveGroup = new Elegant.Ui.RibbonGroup();
            this.MoveUp = new Elegant.Ui.Button();
            this.MoveDown = new Elegant.Ui.Button();
            this.RemoveSelection = new Elegant.Ui.Button();
            this.RemoveAll = new Elegant.Ui.Button();

            this.OptionsGroup = new Elegant.Ui.RibbonGroup();
            this.SetOutputFolder = new OutputFolderHistoryDropDown();
            this.OutputFolderHistory = new Elegant.Ui.PopupMenu();
            this.Bookmark = new Elegant.Ui.CheckBox();
            this.Hyperlink = new Elegant.Ui.CheckBox();
            this.Navigator = new Elegant.Ui.CheckBox();

            this.ActionGroup = new Elegant.Ui.RibbonGroup();
            this.Start = new Elegant.Ui.Button();
            this.Stop = new Elegant.Ui.Button();

            #endregion

            #region Begin initialization
            ((System.ComponentModel.ISupportInitialize)(this.AddGroup)).BeginInit();
            this.AddGroup.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.MoveGroup)).BeginInit();
            this.MoveGroup.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.OptionsGroup)).BeginInit();
            this.OptionsGroup.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.ActionGroup)).BeginInit();
            this.ActionGroup.SuspendLayout();
            #endregion

            #region Initialization
            // 
            // AddGroup
            // 
            this.AddGroup.Controls.Add(this.AddFiles);
            this.AddGroup.Controls.Add(this.AddFolder);
            this.AddGroup.Controls.Add(this.SearchSubFolder);
            this.AddGroup.Controls.Add(this.SearchHiddenFiles);
            this.AddGroup.DialogLauncherButtonVisible = false;
            this.AddGroup.Id = "47097392-ca9d-4fab-8c51-f49f749d4021";
            this.AddGroup.Location = new System.Drawing.Point(4, 3);
            this.AddGroup.Name = "AddGroup";
            this.AddGroup.Size = new System.Drawing.Size(220, 94);
            this.AddGroup.LargeImages.Images.AddRange(new Elegant.Ui.ControlImage[] {
            new Elegant.Ui.ControlImage("Normal", global::iPdf.Properties.Resources.Add)});
            this.AddGroup.SmallImages.Images.AddRange(new Elegant.Ui.ControlImage[] {
            new Elegant.Ui.ControlImage("Normal", global::iPdf.Properties.Resources.Add16)});
            this.AddGroup.TabIndex = 0;
            this.AddGroup.Text = "Add";
            // 
            // AddFiles
            // 
            this.AddFiles.CommandName = "AddFiles";
            this.AddFiles.Id = "13b3a3ce-d1c3-47ba-9cf0-cd7c18f7b4e2";
            this.AddFiles.LargeImages.Images.AddRange(new Elegant.Ui.ControlImage[] {
            new Elegant.Ui.ControlImage("Normal", global::iPdf.Properties.Resources.AddFiles)});
            this.AddFiles.Location = new System.Drawing.Point(6, 0);
            this.AddFiles.Name = "AddFiles";
            this.AddFiles.Size = new System.Drawing.Size(42, 72);
            this.AddFiles.SmallImages.Images.AddRange(new Elegant.Ui.ControlImage[] {
            new Elegant.Ui.ControlImage("Normal", global::iPdf.Properties.Resources.AddFiles16)});
            this.AddFiles.TabIndex = 0;
            this.AddFiles.Text = "Add Files";
            // 
            // AddFolder
            // 
            this.AddFolder.CommandName = "AddFolder";
            this.AddFolder.Id = "8f3e21b0-bbd7-43bc-95c4-7a3d4b8f34f7";
            this.AddFolder.InformativenessMaximumLevel = "Elegant.Ui.RibbonGroupButtonInformativenessLevel:SmallImageWithText";
            this.AddFolder.LargeImages.Images.AddRange(new Elegant.Ui.ControlImage[] {
            new Elegant.Ui.ControlImage("Normal", global::iPdf.Properties.Resources.AddFolder)});
            this.AddFolder.Location = new System.Drawing.Point(48, 0);
            this.AddFolder.Name = "AddFolder";
            this.AddFolder.Size = new System.Drawing.Size(42, 72);
            this.AddFolder.SmallImages.Images.AddRange(new Elegant.Ui.ControlImage[] {
            new Elegant.Ui.ControlImage("Normal", global::iPdf.Properties.Resources.AddFolder16)});
            this.AddFolder.TabIndex = 1;
            this.AddFolder.Text = "Add Files In Folder";
            // 
            // SearchSubFolder
            // 
            this.SearchSubFolder.Id = "9342892c-0d89-4cb4-857b-a4958d711fe4";
            this.SearchSubFolder.Location = new System.Drawing.Point(90, 0);
            this.SearchSubFolder.Name = "SearchSubFolder";
            this.SearchSubFolder.Size = new System.Drawing.Size(114, 24);
            this.SearchSubFolder.TabIndex = 2;
            this.SearchSubFolder.Text = "Search Sub Folder";
            this.SearchSubFolder.Checked = true;
            // 
            // SearchHiddenFiles
            // 
            this.SearchHiddenFiles.Id = "afc7322c-241b-43b0-9cdb-b2333dae4e53";
            this.SearchHiddenFiles.Location = new System.Drawing.Point(90, 24);
            this.SearchHiddenFiles.Name = "SearchHiddenFiles";
            this.SearchHiddenFiles.Size = new System.Drawing.Size(123, 24);
            this.SearchHiddenFiles.TabIndex = 3;
            this.SearchHiddenFiles.Text = "Search Hidden Files";
            // 
            // MoveGroup
            // 
            this.MoveGroup.Controls.Add(this.MoveUp);
            this.MoveGroup.Controls.Add(this.MoveDown);
            this.MoveGroup.Controls.Add(this.RemoveSelection);
            this.MoveGroup.Controls.Add(this.RemoveAll);
            this.MoveGroup.DialogLauncherButtonVisible = false;
            this.MoveGroup.Enabled = false;
            this.MoveGroup.Id = "845e8daa-a5a0-4d41-ae9c-09837b1fdc17";
            this.MoveGroup.LargeImages.Images.AddRange(new Elegant.Ui.ControlImage[] {
            new Elegant.Ui.ControlImage("Normal", global::iPdf.Properties.Resources.Move)});
            this.MoveGroup.Location = new System.Drawing.Point(226, 3);
            this.MoveGroup.Name = "MoveGroup";
            this.MoveGroup.Size = new System.Drawing.Size(198, 94);
            this.MoveGroup.SmallImages.Images.AddRange(new Elegant.Ui.ControlImage[] {
            new Elegant.Ui.ControlImage("Normal", global::iPdf.Properties.Resources.Move16)});
            this.MoveGroup.TabIndex = 1;
            this.MoveGroup.Text = "Move";
            // 
            // MoveUp
            // 
            this.MoveUp.CommandName = "MoveUp";
            this.MoveUp.Id = "c0e765e0-415d-42a5-9eed-d7106522666c";
            this.MoveUp.InformativenessMaximumLevel = "Elegant.Ui.RibbonGroupButtonInformativenessLevel:SmallImageWithText";
            this.MoveUp.LargeImages.Images.AddRange(new Elegant.Ui.ControlImage[] {
            new Elegant.Ui.ControlImage("Normal", global::iPdf.Properties.Resources.MoveUp)});
            this.MoveUp.Location = new System.Drawing.Point(7, 0);
            this.MoveUp.Name = "MoveUp";
            this.MoveUp.Size = new System.Drawing.Size(42, 72);
            this.MoveUp.SmallImages.Images.AddRange(new Elegant.Ui.ControlImage[] {
            new Elegant.Ui.ControlImage("Normal", global::iPdf.Properties.Resources.MoveUp16)});
            this.MoveUp.TabIndex = 0;
            this.MoveUp.Text = "Move Up";
            // 
            // MoveDown
            // 
            this.MoveDown.CommandName = "MoveDown";
            this.MoveDown.Id = "65fa08ec-20f0-4e6a-8649-adb9d3a9b2a8";
            this.MoveDown.InformativenessMaximumLevel = "Elegant.Ui.RibbonGroupButtonInformativenessLevel:SmallImageWithText";
            this.MoveDown.LargeImages.Images.AddRange(new Elegant.Ui.ControlImage[] {
            new Elegant.Ui.ControlImage("Normal", global::iPdf.Properties.Resources.MoveDown)});
            this.MoveDown.Location = new System.Drawing.Point(49, 0);
            this.MoveDown.Name = "MoveDown";
            this.MoveDown.Size = new System.Drawing.Size(42, 72);
            this.MoveDown.SmallImages.Images.AddRange(new Elegant.Ui.ControlImage[] {
            new Elegant.Ui.ControlImage("Normal", global::iPdf.Properties.Resources.MoveDown16)});
            this.MoveDown.TabIndex = 1;
            this.MoveDown.Text = "Move Down";
            // 
            // RemoveSelection
            // 
            this.RemoveSelection.CommandName = "RemoveSelection";
            this.RemoveSelection.Id = "aeab6639-6a92-406b-92e9-9aa8c7ebb174";
            this.RemoveSelection.InformativenessMaximumLevel = "Elegant.Ui.RibbonGroupButtonInformativenessLevel:SmallImageWithText";
            this.RemoveSelection.LargeImages.Images.AddRange(new Elegant.Ui.ControlImage[] {
            new Elegant.Ui.ControlImage("Normal", global::iPdf.Properties.Resources.Remove)});
            this.RemoveSelection.Location = new System.Drawing.Point(91, 0);
            this.RemoveSelection.Name = "RemoveSelection";
            this.RemoveSelection.Size = new System.Drawing.Size(52, 72);
            this.RemoveSelection.SmallImages.Images.AddRange(new Elegant.Ui.ControlImage[] {
            new Elegant.Ui.ControlImage("Normal", global::iPdf.Properties.Resources.Remove16)});
            this.RemoveSelection.TabIndex = 2;
            this.RemoveSelection.Text = "Remove Selection";
            // 
            // RemoveAll
            // 
            this.RemoveAll.CommandName = "RemoveAll";
            this.RemoveAll.Id = "86c8a2f0-e451-4030-a1db-703d280fb624";
            this.RemoveAll.InformativenessMinimumLevel = "Elegant.Ui.RibbonGroupButtonInformativenessLevel:LargeImageWithText";
            this.RemoveAll.LargeImages.Images.AddRange(new Elegant.Ui.ControlImage[] {
            new Elegant.Ui.ControlImage("Normal", global::iPdf.Properties.Resources.RemoveAll)});
            this.RemoveAll.Location = new System.Drawing.Point(143, 0);
            this.RemoveAll.Name = "RemoveAll";
            this.RemoveAll.Size = new System.Drawing.Size(47, 72);
            this.RemoveAll.SmallImages.Images.AddRange(new Elegant.Ui.ControlImage[] {
            new Elegant.Ui.ControlImage("Normal", global::iPdf.Properties.Resources.RemoveAll16)});
            this.RemoveAll.TabIndex = 3;
            this.RemoveAll.Text = "Remove All";
            // 
            // OptionGroup
            // 
            this.OptionsGroup.Controls.Add(this.SetOutputFolder);
            this.OptionsGroup.Controls.Add(this.Bookmark);
            this.OptionsGroup.Controls.Add(this.Hyperlink);
            this.OptionsGroup.Controls.Add(this.Navigator);
            this.OptionsGroup.DialogLauncherButtonVisible = false;
            this.OptionsGroup.Id = "783d147b-8fd5-48d6-b286-265943ad2341";
            this.OptionsGroup.Location = new System.Drawing.Point(181, 3);
            this.OptionsGroup.Name = "OptionGroup";
            this.OptionsGroup.Size = new System.Drawing.Size(148, 94);
            this.OptionsGroup.LargeImages.Images.AddRange(new Elegant.Ui.ControlImage[] {
            new Elegant.Ui.ControlImage("Normal", global::iPdf.Properties.Resources.Option)});
            this.OptionsGroup.SmallImages.Images.AddRange(new Elegant.Ui.ControlImage[] {
            new Elegant.Ui.ControlImage("Normal", global::iPdf.Properties.Resources.Option16)});
            this.OptionsGroup.TabIndex = 1;
            this.OptionsGroup.Text = "Options";
            // 
            // SetOutputFolder
            // 
            this.SetOutputFolder.Id = "9fa42668-a9bf-408d-b8cc-7cf789233fbc";
            this.SetOutputFolder.LargeImages.Images.AddRange(new Elegant.Ui.ControlImage[] {
            new Elegant.Ui.ControlImage("Normal", global::iPdf.Properties.Resources.SetFolder)});
            this.SetOutputFolder.SmallImages.Images.AddRange(new Elegant.Ui.ControlImage[] {
            new Elegant.Ui.ControlImage("Normal", global::iPdf.Properties.Resources.SetFolder16)});
            this.SetOutputFolder.Location = new System.Drawing.Point(4, 2);
            this.SetOutputFolder.Name = "SetOutputFolder";
            this.SetOutputFolder.Popup = this.OutputFolderHistory;
            this.SetOutputFolder.Size = new System.Drawing.Size(63, 72);
            this.SetOutputFolder.TabIndex = 3;
            this.SetOutputFolder.Text = "Set Output Folder";
            // 
            // OutputFolderHistory
            // 
            this.OutputFolderHistory.PlacementMode = Elegant.Ui.PopupPlacementMode.Bottom;
            this.OutputFolderHistory.Size = new System.Drawing.Size(100, 100);
            this.OutputFolderHistory.Visible = true;
            // 
            // Bookmark
            // 
            this.Bookmark.Id = "fab80d01-7591-4c63-9de0-02ced8543f93";
            this.Bookmark.Location = new System.Drawing.Point(69, 2);
            this.Bookmark.Name = "Bookmark";
            this.Bookmark.Size = new System.Drawing.Size(74, 24);
            this.Bookmark.TabIndex = 0;
            this.Bookmark.Text = "Bookmark";
            // 
            // Hyperlink
            // 
            this.Hyperlink.Id = "c053082c-eb22-435b-97e6-8f96b417a9cc";
            this.Hyperlink.Location = new System.Drawing.Point(69, 26);
            this.Hyperlink.Name = "Hyperlink";
            this.Hyperlink.Size = new System.Drawing.Size(71, 24);
            this.Hyperlink.TabIndex = 1;
            this.Hyperlink.Text = "Hyperlink";
            // 
            // Navigator
            // 
            this.Navigator.Id = "22f0a3ba-ff81-472f-8692-d85a471bdf6b";
            this.Navigator.Location = new System.Drawing.Point(69, 50);
            this.Navigator.Name = "Navigator";
            this.Navigator.Size = new System.Drawing.Size(72, 24);
            this.Navigator.TabIndex = 2;
            this.Navigator.Text = "Navigator";

            // 
            // ActionGroup
            // 
            this.ActionGroup.Controls.Add(this.Start);
            this.ActionGroup.Controls.Add(this.Stop);
            this.ActionGroup.DialogLauncherButtonVisible = false;
            this.ActionGroup.Enabled = false;
            this.ActionGroup.Id = "e17cdfdc-2bc2-4cb6-bcfa-efbc1520023b";
            this.ActionGroup.Location = new System.Drawing.Point(498, 3);
            this.ActionGroup.Name = "ActionGroup";
            this.ActionGroup.Size = new System.Drawing.Size(95, 94);
            this.ActionGroup.LargeImages.Images.AddRange(new Elegant.Ui.ControlImage[] {
            new Elegant.Ui.ControlImage("Normal", global::iPdf.Properties.Resources.Action)});
            this.ActionGroup.SmallImages.Images.AddRange(new Elegant.Ui.ControlImage[] {
            new Elegant.Ui.ControlImage("Normal", global::iPdf.Properties.Resources.Action16)});
            this.ActionGroup.TabIndex = 3;
            this.ActionGroup.Text = "Action";
            // 
            // Start
            // 
            this.Start.CommandName = "Start";
            this.Start.EnabledChanged += new EventHandler(
                delegate(object sender, EventArgs e)
                {
                    if (Start.Enabled)
                    {
                        Stop.Enabled = false;
                    }
                });
            this.Start.Id = "d980a991-af2f-4ef4-b1f2-f96fabe1b905";
            this.Start.LargeImages.Images.AddRange(new Elegant.Ui.ControlImage[] {
            new Elegant.Ui.ControlImage("Normal", global::iPdf.Properties.Resources.Start)});
            this.Start.Location = new System.Drawing.Point(5, 0);
            this.Start.Name = "Start";
            this.Start.Size = new System.Drawing.Size(42, 72);
            this.Start.SmallImages.Images.AddRange(new Elegant.Ui.ControlImage[] {
            new Elegant.Ui.ControlImage("Normal", global::iPdf.Properties.Resources.Start16)});
            this.Start.TabIndex = 0;
            this.Start.Text = "Start";
            // 
            // Stop
            // 
            this.Stop.CommandName = "Stop";
            this.Stop.Id = "508e364a-1fb9-4f20-9620-93ae99b0875b";
            this.Stop.LargeImages.Images.AddRange(new Elegant.Ui.ControlImage[] {
            new Elegant.Ui.ControlImage("Normal", global::iPdf.Properties.Resources.Stop)});
            this.Stop.Location = new System.Drawing.Point(47, 0);
            this.Stop.Name = "Stop";
            this.Stop.Size = new System.Drawing.Size(42, 72);
            this.Stop.SmallImages.Images.AddRange(new Elegant.Ui.ControlImage[] {
            new Elegant.Ui.ControlImage("Normal", global::iPdf.Properties.Resources.Stop16)});
            this.Stop.TabIndex = 1;
            this.Stop.Text = "Stop";


            #endregion

            #region End initialization
            ((System.ComponentModel.ISupportInitialize)(this.AddGroup)).EndInit();
            this.AddGroup.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.MoveGroup)).EndInit();
            this.MoveGroup.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.OptionsGroup)).EndInit();
            this.OptionsGroup.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.ActionGroup)).EndInit();
            this.ActionGroup.ResumeLayout(false);
            #endregion

            commonGroups.Add(this.AddGroup);
            commonGroups.Add(this.MoveGroup);
            commonGroups.Add(this.OptionsGroup);
            commonGroups.Add(this.ActionGroup);
        }
        #endregion

        private void TabPage_Change(object sender, EventArgs e)
        {
            if (currentPage == Ribbon.CurrentTabPage)
            {
                return;
            }

            if (Busy || this.previewer.Busy)
            {
                //if we're processing something, we don't allow tab page to be changed
                Ribbon.CurrentTabPage = currentPage;
                return;
            }

            //stop the help info timer first
            helpInfoTimer.Stop();
            tabPageChanging = true;
            previousPage = currentPage;
            currentPage = (TataRibbonPage)Ribbon.CurrentTabPage;
            UpdateSessionsPanel();
            UpdatePageIntroInfo();
            LoadCommonControls();
            currentPage.OnTabPageChanged();
            UpdateGrid();
            UpdateRibbon();
            tabPageChanging = false;
            Status = WorkStatus.Idle;

            //restart the help info timer
            helpInfoTimer.Start();
        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            switch (this.WindowState)
            {
                case FormWindowState.Minimized:
                    ShowMainWindow(false);
                    ShowMinimizeNotification();
                    break;

                case FormWindowState.Maximized:
                case FormWindowState.Normal:
                    this.oldWindowState = this.WindowState;
                    break;
            }

            FitClientControls();
        }

        public string ShowSaveFileDialog(FileFormat[] formats, string defaultFileName)
        {
            if (this.InvokeRequired)
            {
                object[] paras = new object[] { formats, defaultFileName };
                return (string)this.Invoke( new FileDialogCaller( Helper.SaveFile ), paras);
            }
            else
            {
                return Helper.SaveFile(formats, defaultFileName);
            }
        }

        public string ShowChooseDirectoryDialog()
        {
            if (this.InvokeRequired)
            {
                return (string)this.Invoke(new ChooseDirectoryDialogCaller(Helper.ChooseDirectory));
            }
            else
            {
                return Helper.ChooseDirectory();
            }
        }

        private void Grid_DragDrop(object sender, DragEventArgs e)
        {
            IDataObject data = e.Data;
            // Determine whether string data exists in the drop data. If not, then
            // the drop effect reflects that the drop cannot occur.
            if (!data.GetDataPresent(DataFormats.FileDrop))
            {
                return;
            }

            // Extract the data from the DataObject-Container into a string list
            string[] itemList = (string[])data.GetData(DataFormats.FileDrop, false);
            List<string> folderList = new List<string>();
            List<string> fileList = new List<string>();

            foreach (string item in itemList)
            {
                if (File.Exists(item))
                {
                    fileList.Add(item);
                }
                if (Directory.Exists(item))
                {
                    folderList.Add(item);
                }
            }

            AddItemsToGrid(fileList.ToArray());
            if (folderList.Count > 0)
            {
                ItemRetrievor retrievor = new ItemRetrievor(Format,
                                                OnFindingComplete,
                                                OnItemFound);
                retrievor.RetrieveFolders(SearchOption, folderList.ToArray());
            }
        }

        private void Grid_DragEnter(object sender, DragEventArgs e)
        {
            // Check if the Dataformat of the data can be accepted
            // (we only accept file drops from Explorer, etc.)
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
                e.Effect = DragDropEffects.Copy; // Okay
            else
                e.Effect = DragDropEffects.None;
        }

        private void SessionsPanel_Resize(object sender, EventArgs e)
        {
            int widthOffset = this.SessionsPanel.Visible? this.SessionsPanel.Width - this.Grid.Location.X : 0 - this.Grid.Location.X;
            Helper.ShrinkRight(this.Grid, widthOffset);
            Helper.ShrinkRight(this.previewer, widthOffset);

            this.SessionsManager.Height = this.SessionsPanel.Height - this.SessionsManager.Location.Y;
        }

        private void Ribbon_Resize(object sender, EventArgs e)
        {
            FitClientControls();
        }

        private void PrinterStatus_PressedChanged(object sender, EventArgs e)
        {
            if (this.PrinterStatus.Pressed != IsPdfCreatorBusy)
            {
                this.PrinterStatus.Pressed = IsPdfCreatorBusy;
            }
        }
    }
}
